package com.ifcar.app.activity;

import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.fragment.FragmentUtil;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.util.Tools;
import com.ifcar.app.util.ThemeSettingsHelper.ThemeCallback;
import com.ifcar.app.activity.R;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.PopupWindow;

import java.util.HashMap;

public class BaseActivity extends FragmentActivity implements ThemeCallback{
	public int screentype = 3;//屏幕分辨率
	public SharedPreferences sp;
	public MyApplication app;
	public SQLiteDatabase db;
	public int screenwidth = 480;
	public int screenheight = 800;
	public final int swipe_min_distance = 150;
	public final int swipe_max_off_path = 50;
	public final int swipe_threshold_veloicty = 300;
	public static final String PARAM_THEME = "param_theme";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mThemeSettingsHelper = ThemeSettingsHelper.getThemeSettingsHelper(this);
		mThemeSettingsHelper.registerThemeCallback(this, getIntent().getStringExtra(PARAM_THEME));
		
		sp = PreferenceManager.getDefaultSharedPreferences(this);
		app = (MyApplication)getApplicationContext();
		
		db = app.getSQLiteDatabase();
		if(null==db || !db.isOpen()){
			NewsSQLiteOpenHelper sqlOpenHelper = new NewsSQLiteOpenHelper(getApplicationContext());
			db = sqlOpenHelper.getWritableDatabase();
			app.setDb(db);
			app.setSqlOpenHelper(sqlOpenHelper);
		}
		screentype = Tools.getScreenMetrics(this);
		HashMap<String,Integer>  whmap = Tools.getWidth_Height(this);
		screenwidth = whmap.get("w");
		screenheight = whmap.get("h");
	}
	
	
	
	@Override
	protected void onStop() {
		super.onStop();
		mPaused = true;
	}

	

	@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (menuWindow != null && menuWindow.isShowing()) {
            menuWindow.dismiss();
        }
    }



    @Override
	protected void onDestroy() {
		super.onDestroy();
		mThemeSettingsHelper.unRegisterThemeCallback(this);
		if (menuWindow != null && menuWindow.isShowing()) {
		    menuWindow.dismiss();
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mPaused = false;
		
		if (mThemeChanged) {
			applyTheme();
		}
		
	}
	
	private ThemeSettingsHelper mThemeSettingsHelper;
	private boolean mPaused = true;
	private boolean mThemeChanged;
    
    public void applyTheme() {
    	if (mPaused) {
    		mThemeChanged = true;
    	} else {
    	    mThemeChanged = false;
    		onApplyTheme();
    	}
    }
    
    public boolean isThemeChanged() {
        return mThemeChanged;
    }
    
    public boolean isPaused() {
    	return mPaused;
    }
    
    public ThemeSettingsHelper getThemeSettingsHelper() {
    	return mThemeSettingsHelper;
    }
    
    /**
     * 重写此方法,在主题切换时调用
     */
    public void onApplyTheme() {
    }
	
    protected PopupWindow menuWindow;
    
	/**
	 * 此方法是对增加fragment方便调用
	 */
	public Fragment addFragmentByTag(int container, Class clazz, String tag,
			Bundle argument) {
		return addFragmentByTag(container, clazz.getName(), tag, argument);

	}

	@Override
	public boolean onSearchRequested() {
		// TODO Auto-generated method stub
		startActivity(new Intent(this, SearchNewsActivity.class));
		return true;
	}
	
	/**
	 * 此方法是对增加fragment方便调用
	 */
	public Fragment addFragmentByTag(int container, String clazz, String tag,
			Bundle argument) {
		FragmentManager fm = getSupportFragmentManager();

		Fragment f = fm.findFragmentByTag(tag);
		if (f == null) {
			FragmentTransaction ft = fm.beginTransaction();
			f = Fragment.instantiate(this, clazz, argument);
			if (container == 0) {
				ft.add(f, tag);
			} else {
				ft.add(container, f, tag);
			}

			FragmentUtil.commitAndExec(fm, ft, false);
		} else if (f.isDetached()) {
            FragmentTransaction ft = fm.beginTransaction();
            ft.attach(f);
            FragmentUtil.commitAndExec(fm, ft, false);
        }
		
		return f;
	}
    
    public void hideIcon() {
        View v = findViewById(R.id.top_id_image);
        if (v != null) {
            v.setVisibility(View.GONE);
        }
    }
    
    public void showIcon() {
        View v = findViewById(R.id.top_id_image);
        if (v != null) {
            v.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        addThemeIntent(intent);
        super.startActivityForResult(intent, requestCode);
    }

    @Override
    public void startActivityFromFragment(Fragment fragment, Intent intent, int requestCode) {
        addThemeIntent(intent);
        super.startActivityFromFragment(fragment, intent, requestCode);
    }

    @Override
    public void startActivity(Intent intent) {
        addThemeIntent(intent);
        super.startActivity(intent);
    }



    @Override
    public boolean startActivityIfNeeded(Intent intent, int requestCode) {
        addThemeIntent(intent);
        return super.startActivityIfNeeded(intent, requestCode);
    }



    private void addThemeIntent(Intent intent) {
        if (intent == null) {
            return;
        }
        String theme = getIntent().getStringExtra(PARAM_THEME);
        if (!TextUtils.isEmpty(theme) && TextUtils.isEmpty(intent.getStringExtra(PARAM_THEME))) {
            intent.putExtra(BaseActivity.PARAM_THEME, theme);
        }
    }

    @Override
    public Context getContext() {
        return this;
    }
}
